-- fail.lua
-- Tells someone they fail like something random
-- Version 1.0
-- By Tyrone

---------------------------------------

--[[
Changelog:
1.0
-Basic functionality, 3 part sentence

]]



-- set up all command prefixes
cmdNames = {
    "fail"
}


COOLDOWN = 30 -- plugin cooldown, 0 for none
_,bot_name = VH:GetConfig("config", "hub_security")
bot_desc = "bot description here"
bot_email = "bot@nowhere.com"
use_bot = false

function doCommand(nick, data, z)

	if (ready(nick)) then
		return true
	end

	if (data == nil) then
		SendMessageToHub(nick..getFailString().." because they can't work out .fail's syntax.", bot_name)
	else
		SendMessageToHub(data..getFailString()..".", bot_name)
	end
end

function doHelp(nick)
    SendMessageToUser("Usage: ", nick, bot_name)
    DisplayHelpMessage("help", "Shows this message.", nick)
    -- put coad in here for help commands:
	DisplayHelpMessage("", "Shows you the last thing you said", nick)

end

-- insert other functions here
-- User fails like <a/an thing> with <an/a object> <at/on a/an location>

function getFailString()
	rnd = math.random(1, table.getn(thing))
	str = ""
	str = str.." fails like "..thing[rnd].." with "
	rnd = math.random(1, table.getn(object))
	str = str..object[rnd].." "
	rnd = math.random(1, table.getn(location))
	str = str..location[rnd]
	return str
end
	
thing = {	"a prostitute", "a camel", "a redneck", "an azn", "heinko",
			"your mother", "bill gates", "HAL 9000", "a midget", "an S&S lecture",
			"console", "your dad", "windows vista", "debbie"}
			
object = {	"void's car", "a game of dota", "paliwal", "drugs", "a tiny penis",
			"lolicon", "hentai", "a mac", "a rocking chair", "a gynaecologist",
			"an espresso machine", "a cupcake"}
			
location = {"at sonik's flat",	"at an anime convention", "at the klingon homeworld", "at the pub",
			"on the moon", "at the hubbit", "in the city", "at a country women's association meeting",
			"at alcoholics anonymous", "on the love boat with men", "in a big brother episode",
			"on survivor", "staying at BK", "at sunnybank", "on the intarwebz"}
---------------------------------------
-- you can ignore below here
time = 0

function Main()
    -- Check if script should add a new bot.
    if (use_bot) then
    -- Add new bot.
        VH:AddRobot(bot_name, 10, bot_desc, "Bot ", bot_email, "0")
    end
end

function UnLoad()
	if (use_bot) then
    	VH:DelRobot(bot_name)
	end
end

function VH_OnParsedMsgChat(nick, data)
	if (string.find(data, "^%."..cmdNames[1])) then
		_,_,cmd = string.find(data, "^%."..cmdNames[1].."%s(.+)")
		if (cmd == "help") then
		    doHelp(nick)
		else
        	doCommand(nick, cmd, cmdNames[1])
        end
    	return 0
	end
	return 1
end

function ready(nick)
    if (time == 0) then
		time = COOLDOWN
	elseif (time == nil) then
		time = COOLDOWN
	else
		SendMessageToUser("This command can only be used every "..COOLDOWN.." seconds.  You have "..time.." seconds to wait.", nick, bot_name)
		return true
	end
	return false
end

function SendMessageToHub(data, from)
	return VH:SendDataToAll("<"..from.."> "..data.."|",0,10)
end

function SendMessageToUser(data, nick, from)
	return VH:SendDataToUser("<"..from.."> "..data.."|", nick)
end

function SendPrivateMessageToUser(user, data, from)
    return VH:SendDataToUser("$To: "..user.." From: "..from.." $<"..from.."> "..data.."|", user);
end

function DisplayHelpMessage(cmd, msg, nick)
    SendMessageToUser("        ."..cmdNames[1].." "..cmd, nick, bot_name)
    SendMessageToUser("        "..msg, nick, bot_name)
end

function UserIsOnline(nick)
	result, sIP = VH:GetUserIP(nick)
	if (string.len(sIP) < 7) then
		return false
	else
		return true
	end
end

function UserIP(nick)
	result, sIP = VH:GetUserIP(nick)
	if (string.len(sIP) < 7) then
		return 0
	else
		return sIP
	end
end

function UserClass(nick)
	res, class = VH:GetUserClass(nick)
	if res and class then return class else return 0 end
end

function SendEmoteToHub(data, from)
	result, err = VH:SendDataToAll("<"..from.."> ".."/me "..data.."|",0,10)
	return 1
end

function VH_OnTimer()
	if (time > 0) then
		time = time - 1
	end
	return 1
end